using System.Collections.Generic;
using OracleInternal.Common;

namespace OracleInternal.MTS
{
	internal class TxnBranchesByString : SyncDictionary<string, MTSTxnBranches>
	{
		private object m_lock = new object();

		internal int BranchCount
		{
			get
			{
				int num = 0;
				List<string> keys = GetKeys();
				MTSTxnBranches mTSTxnBranches = null;
				for (int i = 0; i < keys.Count; i++)
				{
					mTSTxnBranches = base[keys[i]];
					if (mTSTxnBranches != null)
					{
						num += mTSTxnBranches.Count;
					}
				}
				return num;
			}
		}

		internal void ClearBranches()
		{
			List<string> keys = GetKeys();
			MTSTxnBranches mTSTxnBranches = null;
			for (int i = 0; i < keys.Count; i++)
			{
				base[keys[i]]?.ClearBranches();
			}
			Clear();
		}

		internal void EnqueueBranch(string dbInstance, MTSTxnBranch txnBranch)
		{
			MTSTxnBranches mTSTxnBranches = null;
			if ((mTSTxnBranches = base[dbInstance]) == null)
			{
				lock (m_lock)
				{
					if ((mTSTxnBranches = base[dbInstance]) == null)
					{
						mTSTxnBranches = (base[dbInstance] = new MTSTxnBranches());
					}
				}
			}
			mTSTxnBranches.AddIfNotExist(txnBranch);
		}

		internal bool DequeueBranch(string dbInstance, out MTSTxnBranch txnBranch)
		{
			MTSTxnBranches mTSTxnBranches = null;
			txnBranch = null;
			if ((mTSTxnBranches = base[dbInstance]) == null)
			{
				return false;
			}
			return mTSTxnBranches.Dequeue(out txnBranch);
		}
	}
}
